package com.example.pptcontrol;


import android.os.Handler;
import android.os.Message;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;

import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;

public class GestureListener extends SimpleOnGestureListener
{
	public Handler mUILooperHandler;
	public GestureListener()
	{
	}
	
	public boolean onScroll( MotionEvent e1, MotionEvent e2, float distanceX, float distanceY )
	{
		return false;
		// return handleMovement(distanceX, distanceY);
	}
	
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        return handleMovement(velocityX, velocityY);
    }
   
    JSONObject requestObj = new JSONObject();
    private boolean handleMovement(float dX, float dY)
    {    
    	requestObj = new JSONObject();
    	Log.i(GestureListener.class.getName(),dX+":"+dY);
        // Need to decide if the principal scroll direction is horizontal

    	try 
    	{
    		if ( dX > 50 )
        	{
      			requestObj.put("Operation", "Right");
        	}
        	else if(dX<-50)
        	{
        		requestObj.put("Operation", "Left");
        	}
        	else
        	{
        		return false;
        	}
    		new Thread(new Runnable() {
				
				@Override
				public void run() 
				{
					try 
					{
						HttpManager http = new HttpManager();
		        		byte[] requestBody =requestObj.toString().getBytes(Constants.UTF8) ;
		        		HttpResponse result = http.post("http://10.104.151.37/webhost/ControlRequest.aspx", null,requestBody);
		        		String respoString = new String(result.getBody());
		        		Log.i(GestureListener.class.getName(), respoString);
		        		Message msgMessage= new Message();
		        		msgMessage.obj = respoString;
		        		mUILooperHandler.sendMessage(msgMessage);
					} 
					
	        		catch (Exception e) 
	            	{
	    				Log.e(GestureListener.class.getName(), e.toString(),e);
	    				Log.e(GestureListener.class.getName(), e.getMessage());

	    			}

					
				}
			}).start();
		} 
		
		catch (Exception e) {
			
		}
    	
    	return true;

        
        
    }
}
